package com.fluxtool.utill.scheduler;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.fluxtool.entity.Fluxlog;
import com.fluxtool.entity.Ips;
import com.fluxtool.entity.Quartzs;
import com.fluxtool.service.FluxLogService;
import com.fluxtool.service.IpsService;
import com.fluxtool.service.QuartzsService;
import com.fluxtool.utill.date.DateUtill;
import com.fluxtool.utill.springutill.SpringUtill;

@Component
@Configurable
@EnableScheduling
public class SpringBootCoreJob {

	private static final Logger log = LoggerFactory.getLogger(SpringBootCoreJob.class);
	
	@Autowired
	private QuartzsService quartzsService;
	@Autowired
	private IpsService ipsService;
	@Autowired
	private FluxLogService fluxLogService;
	
	private int flag = 0;
	
	@Scheduled(cron="0 0/60 * * * * ")
	public void quartzMain() {
		
		if(quartzsService==null || ipsService==null || fluxLogService==null){
			quartzsService = SpringUtill.getBean(QuartzsService.class);
			ipsService = SpringUtill.getBean(IpsService.class);
			fluxLogService = SpringUtill.getBean(FluxLogService.class);
		}
		
		log.info("*****开始查询已经启动的任务******");  
		List<Quartzs> quartzlist = quartzsService.selectAllWhereEnble();
		List<Ips> ipsList = ipsService.selectAllWhereEnble();
		log.info("*****开始执行符合条件的任务******");
		if(quartzlist.size()>0 && ipsList.size()>0){
			for(Quartzs quartz : quartzlist){
				if(quartz.getTargetTime()==DateUtill.getNowHours()){
					//2.设置ip代理
					int count = 1;
					//根据设定循坏
					for(int i=0; i<quartz.getTargetCount(); i++){
						for(Ips ip : ipsList) {
							if(Agent(ip.getIp().trim(), Integer.valueOf(ip.getPort().trim()), quartz.getTargetUrl(), count)){
								log.info("*****成功代理，"+quartz.getTargetUrl()+"访问次数为"+count+"******");
								count++;
							}
							fluxLogService.insert(new Fluxlog(ip.getIp().trim(), ip.getPort().trim(), quartz.getTargetUrl(), DateUtill.getNowDate(), flag));
						}
					}
				}else{
					log.info("*****没有符合时间的的quartz******");
				}
			}
		}else{
			log.info("*****没有符合的ip或者没有符合的quartz******");
		}
	}
	
	/**
	 * 为了方便记录日志，代理方法独立出来
	 */
	public boolean Agent(String ip, int port, String targetUrl,int count){
		/**
		 * 通过代理对象连接
		 * @param address
		 * @return
		 */
		SocketAddress addr = new InetSocketAddress(ip, port); 
		Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
		try{
			log.info("*****通过代理对象开始连接#"+ip+":"+port+"*****");
		    URL url = new URL(targetUrl);
		    URLConnection conn = url.openConnection(proxy);
		    conn.setConnectTimeout(5000);
		    conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");
		    conn.getContent();
		    flag = 1;
		    return true;
		}catch (Exception e) {
			log.info("*****代理对象连接失败#"+ip+":"+port+"*****");
		    //e.printStackTrace();
		}
		return false;
	}

}
